package logic

import (
	"context"
	"gitignore/pkg"
	"net/http"
	"path/filepath"

	"gitignore/api/internal/svc"
	"gitignore/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UploadLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogic {
	return &UploadLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UploadLogic) Upload(req *http.Request) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	file, m, err := req.FormFile("file")
	if err != nil {
		return &types.Response{
			Message: "上传失败",
			Data:    nil,
			Code:    500,
		}, nil
	}

	uploads, err := pkg.Upload(file, m)
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Data:    nil,
			Code:    500,
		}, nil
	}
	if m.Size <= 5*1024*1024 {
		return &types.Response{
			Message: "图片文件大小≤5MB",
			Data:    nil,
			Code:    500,
		}, nil
	}

	ext := filepath.Ext(m.Filename)
	if ext == ".JPG" || ext == ".PNG" || ext == "WEBP" {
		return &types.Response{
			Message: "类型不同",
			Data:    nil,
			Code:    500,
		}, nil
	}

	exx := filepath.Ext(m.Filename)
	if exx == ".MP4" || exx == "AVI" {
		return &types.Response{
			Message: "类型不同",
			Data:    nil,
			Code:    500,
		}, nil
	}

	if m.Size <= 200*1024*1024 {
		return &types.Response{
			Message: "视频文件大小≤200MB",
			Data:    nil,
			Code:    500,
		}, nil
	}

	return &types.Response{
		Message: "上传成功",
		Data:    uploads,
		Code:    200,
	}, nil
}
